# 资源池化场景kill主要进程，测试可靠性
ps=(cm_server dss gaussdb om_)
count=300
kill_count=5
log_post=$(date "+%Y%m%d_%H_%M_%S")

#查询状态
query_state(){
        last_value="1"
        # 检查半小时
        for((jj=1;jj<600;jj++))
        do
                value="1"
                for k in $(cm_ctl query -Cvwdip | grep -A 10 "Datanode State" | awk '{print $9}')
                do
                        if [ "$k" != "Normal" ] ;then
                                value="0"
                                break
                        fi
                done
                last_vaule=$value
                if [ $value == "1" ]; then
                        break
                fi
                sleep 3
        done
        return $last_value

}

# 执行脚本
for((i=1;i<$count;i++))
do
        for p in ${ps[@]}
        do
                echo "-*-*-*-*begin in $(date)  $i kill $p process-*-*-*-*" >> kill_process-$log_post.log
                cm_ctl query -Cvipw >> kill_process-$log_post.log
                for((j=1;j<$kill_count;j++))
                do
                        ps ux |grep $p | grep -v grep | awk '{print $2}'|xargs kill -9;sleep 1
                done
                echo "------------$(date) process--------" >> kill_process-$log_post.log
                ps ux >> kill_process-$log_post.log
                query_state
                if [ $? == "1" ]; then
                        cm_ctl query -Cvipw >> kill_process-$log_post.log
                        echo "*********begin in $(date) stop start operate*******" >> kill_process-$log_post.log
                        cm_ctl stop;sleep 5;cm_ctl start >> kill_process-$log_post.log
                        cm_ctl query -Cvipw >> kill_process-$log_post.log
                        echo "*********begin in $(date) switchover operate*******" >> kill_process-$log_post.log
                        cm_ctl switchover -a >> kill_process-$log_post.log
                else
                        exit
                fi
        done
        sleep 180
done &
